package scau.erps.view.decision.card;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import scau.erps.app.Erps;
import scau.erps.entity.Sand;
import scau.erps.service.SandService;
import scau.erps.service.impl.SandServiceImpl;
/**
 * 卡片容器模板类（小组决策步骤）
 * @author DCJ
 *
 */
public abstract class CardPanelTemplate extends JPanel implements ActionListener {
	private static final long serialVersionUID = 1L;
	protected JPanel cardPanel,actionPanel,sandPanel;
	protected JTextArea ruleTextArea,infoTextArea;
	protected JButton[] btn = new JButton[33];
	public CardPanelTemplate(){
		actionPanel = new JPanel(); //操作按钮面板
		this.initButton();
		for (int i = 0; i < btn.length; i++) {
			actionPanel.add(btn[i]);			
		}
		sandPanel = new JPanel(); //沙盘模拟面板
		sandPanel.setPreferredSize(new Dimension(700,340));
		sandPanel.setBorder(BorderFactory.createTitledBorder("盘面数据"));
		ruleTextArea = new JTextArea(); //决策需要的业务规则，根据具体情况确定是否显示在sandPanel中
		ruleTextArea.setText(setRule());
		ruleTextArea.setBorder(BorderFactory.createTitledBorder("业务规则"));
		ruleTextArea.setLineWrap(true);
		ruleTextArea.setPreferredSize(new Dimension(700,120));
		cardPanel = new JPanel();
		cardPanel.setLayout(new BorderLayout());
		cardPanel.add(actionPanel,BorderLayout.NORTH);
		cardPanel.add(sandPanel,BorderLayout.CENTER);
		cardPanel.add(ruleTextArea,BorderLayout.SOUTH);
		this.add(cardPanel);
	}
	private void initButton(){		
		String[] str = new String[]{"年度规划","支付应付税","投放广告","登记订单","更新短贷","短贷还本付息","申请短贷","更新应付款","归还应付款",
				"更新原材料订单","原材料入库","下原材料订单","更新生产","完工入库","投资新生产线","生产线转产","变卖生产线","开始下一批生产","产品研发投资"
				,"更新应付款","应付款收现","按订单交货","支付管理费","更新长贷","长贷还本付息","申请长贷","支付设备维修费","支付租金","购买厂房","折旧","新市场开拓",
				"ISO认证投资","关帐"};
		for (int i = 0; i < btn.length; i++) {
			btn[i] = new JButton(str[i]);
			btn[i].setEnabled(false);
			btn[i].setVisible(false);
			btn[i].addActionListener(this);			
		}
		int step = Integer.parseInt(Erps.getSand().getStep());
		int btnIndex = step-1;
		btn[btnIndex].setEnabled(true);
	}
	protected void changeStep(){
		Sand sand = Erps.getSand();
		int currentStep = Integer.parseInt(sand.getStep());
		int nextStep = currentStep+1;
		String step = Integer.toString(nextStep);
		sand.setStep(step);
		SandService sandService = Erps.getIocInstance().get(SandServiceImpl.class);
		sandService.updateSand(sand);
		this.btn[currentStep-1].setEnabled(false);
		this.btn[currentStep].setEnabled(true);
	}
	public abstract String setRule();
}
